package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.query.DestinationQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Autowired
    private IDestinationService destinationService;
    /*@Autowired
    private IDestinationService destinationService;*/

    /**
     * 处理分页和显示数据
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")DestinationQuery qo){
        //toasts,导航吐司需要的信息
        List<Destination> toasts = destinationService.getToast(qo.getParentId());
        //设置共享
        model.addAttribute("toasts",toasts);
        //此处分页的Page对象不是之前mybatis里面的PageInfo对象
        //他是spring-data-mongodb 这个组件封装对象.
        Page<Destination> page = destinationService.query(qo);
        model.addAttribute("page", page);
        return "destination/list";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        destinationService.delete(id);
        return  JsonResult.success();
    }

    @RequestMapping("/updateInfo")
    @ResponseBody
    public Object updateInfo (Destination destination){
        Destination destination1 = destinationService.saveOrUpdate(destination);
        return JsonResult.success(destination1);
    }



}